//
//  BLCommendViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/5/26.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import MJRefresh

class BLCommendViewController: UITableViewController {
    
    let commendVerticalCellID = "commendVerticalCellID"
    let commendLiveRoomCellID = "commendLiveRoomCellID"
    
    //自定义直播数据
    lazy var liveRoomDataArray = [BLLiveRoomItem]()
}

// MARK: - 生命周期
extension BLCommendViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        /// 设置UI
        setupUI()
        //等待通知
        dealNotification()
        //请求数据
        getLiveRoomData()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        /// 通知移除
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}

// MARK: - 设置UI
extension BLCommendViewController{
    
    /// 设置UI
    func setupUI() {
        setupTableView()
    }
    
    /// 设置主tableView
    func setupTableView() {
        //去掉难看的分割线
        tableView.separatorStyle = .None
        //设置头部标题
        tableView.tableHeaderView = BLCommendHeaderTopView(frame:CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 160) )
        tableView.backgroundColor = UIColor.clearColor()
        //注册Cell
//        tableView.registerNib(UINib.init(nibName: String(BLCommendVerticalCell), bundle: nil), forCellReuseIdentifier: commendVerticalCellID)
        tableView.registerNib(UINib.init(nibName: String(BLCommendLiveRoomCell), bundle: nil), forCellReuseIdentifier: commendLiveRoomCellID)
        
        //设置下拉刷新
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.getLiveRoomData()
        })
    }
}

// MARK: - 通知事件与通知事件处理
extension BLCommendViewController{
    
    //等待通知跳转控制器
    func dealNotification() {
        
        //跳转到直播
        NSNotificationCenter.defaultCenter().addObserverForName(kPushLiveRoomNotification, object: nil, queue: NSOperationQueue.currentQueue()) { (notic : NSNotification) in
            
            guard  notic.object != nil else{
                return
            }
            let roomVC = BLRoomViewController()
            roomVC.roomID = notic.object as! String
            self.navigationController?.pushViewController(roomVC, animated: true)
        }
        
        //跳转到手机直播
        NSNotificationCenter.defaultCenter().addObserverForName(kPushPhoneRoomNotification, object: nil, queue: NSOperationQueue.currentQueue()) { (notic : NSNotification) in
            
            guard  notic.object != nil else{
                return
            }
            let roomVC = BLPhoneLiveRoomViewController()
            roomVC.roomID = notic.object as! String
            self.navigationController?.pushViewController(roomVC, animated: true)
            
        }
        
        //跳转到子频道全部直播列表
        NSNotificationCenter.defaultCenter().addObserverForName(kGetMoreNotification, object: nil, queue: NSOperationQueue.currentQueue()) { (notic : NSNotification) in
            let tagID = notic.object as? NSNumber
            let subListVC = BLSubListViewController.subListVC()
            subListVC.tagID = tagID
            subListVC.navTitleText = notic.userInfo![kGetMoreNotificationID] as! String
            self.navigationController?.pushViewController(subListVC, animated: true)
        }
    }
}

// MARK: - tableView代理方法与数据源
extension BLCommendViewController{
    
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if liveRoomDataArray.count != 0 {
            return liveRoomDataArray.count + 1
        }else{
            return liveRoomDataArray.count
        }
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//        
//        if indexPath.item == 0 {
//            let cell = tableView.dequeueReusableCellWithIdentifier(commendVerticalCellID, forIndexPath: indexPath)
//            return cell
//            
//        }
//        else {
            let cell = tableView.dequeueReusableCellWithIdentifier(commendLiveRoomCellID, forIndexPath: indexPath) as! BLCommendLiveRoomCell
            cell.liveRoomitem = liveRoomDataArray[indexPath.item]
            return cell
//        }
        
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.item == 0 {
            return BL_SCREEN_WIDTH + 40
        }
        else{
            return BL_SCREEN_WIDTH - 80
        }
    }
}

// MARK: - 获取网络数据
extension BLCommendViewController{
    //获取颜值room数据
    func getLiveRoomData(){
        BLGetDouyuData.shareInstance.getCustomRoomData { (dataArray) in
            self.liveRoomDataArray = dataArray
            //结束下拉刷新
            self.tableView.mj_header.endRefreshing()
            //设置与刷新视图
            self.tableView.reloadData()
        }
    }
}
